package com.iccaplayer;

import com.iccaplayer.sequencer.Sequencer;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ToggleButton;

public class SamplerToggleListener implements OnClickListener {
	static final String TAG = "SamplerToggleListener";

    Sequencer sequencer;

    int beats;

    public SamplerToggleListener(Context ctx, Sequencer sequencer, int samples, int beats) {
        this.sequencer = sequencer;
        this.beats = beats;
    }

    @Override
    public void onClick(View v) {
        int buttonId = v.getId();

        int samplerId = (buttonId-this.beats) / this.beats;
        int beatId = (buttonId-this.beats) % this.beats;
        Log.d(TAG, "button: " + buttonId + " sample: " + samplerId + " beatId: " + beatId);
        if (!(v instanceof ToggleButton)) {
            Log.e("SampleToggleListener", "Invalid View type: " + v.getClass());
            return;
        }
        ToggleButton currentButton = (ToggleButton) v;
        if (currentButton.isChecked()){
            sequencer.enableCell(samplerId, beatId);
            Log.d(TAG, "(" + samplerId + ", "+ beatId + ") is enabled");
        }
        else{
            sequencer.disableCell(samplerId, beatId);
            Log.d(TAG, "(" + samplerId + ", "+ beatId + ") is disabled");
        }
        
        if(sequencer.getCellKey(samplerId, beatId) == null){
//        	Log.d(TAG, "insPos: " + pos + " sampleId: " + samplerId + " beatId: " + beatId);
        	currentButton.setChecked(false);
        	currentButton.performLongClick();
        }
    }
}
